package com.qinsong.myapplication.controller.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.qinsong.myapplication.R;
import com.qinsong.myapplication.controller.activity.LoginActivity;
import com.qinsong.myapplication.model.Model;

/**
 * 设置列表页面
 */
public class SettingFragment extends Fragment {
    private Button bt_setting_out;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_setting,null);
        //初始化
        InitView(view);
        return view;
    }

    private void InitView(View view) {
        bt_setting_out=view.findViewById(R.id.bt_setting_out);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        //在button上显示当前用户名称
        bt_setting_out.setText("退出登录 ("+EMClient.getInstance().getCurrentUser() +")");
        //退出登录按钮
        bt_setting_out.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Model.getInstance().getGlobaThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        //登录环信的服务器退出
                        EMClient.getInstance().logout(false, new EMCallBack() {
                            @Override
                            public void onSuccess() {
                                //关闭数据库
                                Model.getInstance().getDbManager().close();
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        //回到登录界面
                                        Toast.makeText(getActivity(),"退出成功",Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(getActivity(),LoginActivity.class);
                                        startActivity(intent);
                                        getActivity().finish();
                                    }
                                });


                            }

                            @Override
                            public void onError(int i, final String s) {
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        //回到登录界面
                                        Toast.makeText(getActivity(),"退出失败"+s,Toast.LENGTH_SHORT).show();

                                    }
                                });
                            }

                            @Override
                            public void onProgress(int i, String s) {

                            }
                        });
                    }
                });
            }
        });
    }
}
